<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" >
            <div class="layui-form-item">
                <label for="" class="layui-form-label">任务名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" placeholder="请输入任务名称" lay-verify="required" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="" class="layui-form-label">计划日期</label>
                <div class="layui-input-inline">
                    <input type="text" name="datetime_init" id="datetime_init" lay-verify="required" readonly placeholder="请选择" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">-</div>
                <div class="layui-input-inline">
                    <input type="text" name="datetime_end" id="datetime_end" lay-verify="required" readonly placeholder="请选择" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="" class="layui-form-label">计划时间</label>
                <div class="layui-input-inline">
                    <input type="text" name="daytime" id="daytime" lay-verify="required" readonly placeholder="请选择" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="" class="layui-form-label">客户表单</label>
                <div class="layui-input-inline">
                    <select name="form" lay-verify="required">
                        <option value=""></option>
                        <?php foreach ($forms as $t){ ?>
                            <option value="<?php echo $t['id']; ?>"><?php echo $t['nombre']; ?></option>
                        <?php } ?>
                    </select>
                </div>
                <?php if (empty($forms)){ ?>
                    <div class="layui-form-mid layui-word-aux">
                        <span style="color: darkred">无可用表单，请到客户表单添加</span>
                    </div>
                <?php } ?>
            </div>
            <div class="layui-form-item">
                <label for="" class="layui-form-label">中继线路</label>
                <div class="layui-input-inline">
                    <select name="trunk" lay-verify="required">
                        <option value=""></option>
                        <?php foreach ($trunks as $t){ ?>
                            <option value="<?php echo 'SIP/'.$t['channelid']; ?>"><?php echo 'SIP/'.$t['channelid']; ?></option>
                        <?php } ?>
                    </select>
                </div>
                <?php if (empty($trunks)){ ?>
                <div class="layui-form-mid layui-word-aux">
                    <span style="color: darkred">无可用中继线路，请到中继管理添加</span>
                </div>
                <?php } ?>
            </div>
            <div class="layui-form-item">
                <label for="" class="layui-form-label">队列</label>
                <div class="layui-input-inline">
                    <select name="queue" lay-verify="required" lay-filter="queue">
                        <option value=""></option>
                        <?php foreach ($queues as $t){ ?>
                            <option value="<?php echo $t['extension']; ?>"><?php echo $t['descr']; ?></option>
                        <?php } ?>
                    </select>
                </div>
                <?php if (empty($queues)){ ?>
                    <div class="layui-form-mid layui-word-aux">
                        <span style="color: darkred">无可用队列，请到队列管理添加</span>
                    </div>
                <?php } ?>
            </div>
            <div class="layui-form-item">
                <label for="" class="layui-form-label">最大并发</label>
                <div class="layui-input-inline">
                    <input type="number" name="max_canales" value="0" placeholder="请输入最大并发" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">0表示无限制</div>
            </div>
            <div class="layui-form-item">
                <label for="" class="layui-form-label">重试次数</label>
                <div class="layui-input-inline">
                    <input type="number" name="retries" value="1" placeholder="请输入重试次数，默认为1" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="submitForm">确认</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>


<?php
$url = yii\helpers\Url::to(['create']);
$max_channels = yii\helpers\Url::to(['get-max-channels-by-queue']);
$min = date('Y-m-d');
$js = <<<JS

    layui.use(['form','element','table', 'layer','laydate'],function() {
        var form = layui.form;  
        var element = layui.element;  
        var table = layui.table;  
        var layer = layui.layer;  
        var laydate = layui.laydate;  
        
        var min_date = '{$min}'
        
        var startDate = laydate.render({
            elem:'#datetime_init',
            min:min_date,
            done: function (value, date) {
                endDate.config.min ={
                    year:date.year,
                    month:date.month-1, 
                    date: date.date,
                };
            }
        })
        var endDate = laydate.render({
            elem:'#datetime_end',
            min:min_date,
            done: function (value, date) {
                startDate.config.max = {
                    year: date.year,
                    month: date.month - 1,
                    date: date.date,
                }
            }
        })

        
        
        laydate.render({
            elem: '#daytime'
            ,type: 'time'
            ,range: true
        });
        
        //监听表单提交
        form.on('submit(submitForm)',function(data) {
            var index = layer.load()
            var where = data.field
            $.post("{$url}",where,function(res) {
                layer.close(index)
                layer.msg(res.msg,{},function() {
                    if (res.code==0){
                        location.reload();
                    }   
                })   
            })
            return false
        })
        
        //监听队列设置最大并发
        form.on('select(queue)', function(data){
            if (data.value){
                $.post('{$max_channels}',{extension:data.value},function(res) {
                    if (res.code==0){
                        $("input[name='max_canales']").val(res.data)
                    } else {
                        layer.msg(res.msg)
                    }  
                })
            } else {
                $("input[name='max_canales']").val(0)
            }
        }); 
        
    });
JS;
$this->registerJs($js);
